import Vue from 'vue';
import VueRouter from 'vue-router';

const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;

// 处理路由跳转会报错的问题
VueRouter.prototype.push = function (...rest) {
	return originalPush.apply(this, rest).catch(err => err);
};
VueRouter.prototype.replace = function (...rest) {
	return originalReplace.apply(this, rest).catch(err => err);
};

Vue.use(VueRouter);

const routes = [
	{
		path: '/',
		name: 'home',
		component: () => import('./view/home/index.vue'),
		meta: {
			title: '主页面'
		}
	},
];

// add route path
routes.forEach(route => {
	if (!route.path) {
		route.path = route.path || '/' + (route.name || '');
	}
});

const router = new VueRouter({
	routes
});

router.beforeEach((to, from, next) => {
	next();
});

router.onError(error => {
	if (error.name === 'ChunkLoadError') {
		app.$dialog
			.alert({
				title: '提示',
				message: '前端已发布更新，请退出轻应用重新进入'
			});
	}
});

export { router };
